import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/login'  // 确保默认跳转到登录页
  },
  {
    path: '/login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/materials',
    component: () => import('@/views/MaterialManagement.vue')
  },
  {
    path: '/machines',
    component: () => import('@/views/MachineManagement.vue')
  },
  {
    path: '/orders',
    component: () => import('@/views/OrderManagement.vue')
  },
  {
    path: '/scheduling',
    component: () => import('@/views/Scheduling.vue')
  },
  {
    path: '/schedule-result',
    component: () => import('@/views/ScheduleResult.vue')
  },
  {
    path: '/schedule-plan/:planId',
    component: () => import('@/views/SchedulePlanDetail.vue')
  },
  {
    path: '/main',
    name: 'MainPage',
    component: () => import('../views/MainPage.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/stocks',
    component: () => import('@/views/StockManagement.vue')
  },
  {
    path: '/',
    redirect: '/main'
  },
  {
    path: '/crafts',
    component: () => import('@/views/CraftManagement.vue') // 确保路径正确
  },
  {
    path: '/test-gantt',
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to) => {
  if (to.path === '/login') return true
  
  if (!localStorage.getItem('token')) {
    return '/login'
  }
  return true
})

export default router